Newton Script Examples
----------------------


Here are the most asked for examples. They realy do work as they have been typed into a MP2k and executed.


Example #1 - Put the date into a field so that later it can be used in calculations or sorted.

"RunScript always" is checked.

If RecordAction = 'NewMain then begin   // only execute this part if a new record is made.
     local nTime := time();                            // get the time in minutes
     local TTime := :ToString(nTime);                  // make time into text
     local DbRecord := :GetEntry(CurrentDbCursor);     // get the current record being displayed
     :SetField(CurrentDb, DbRecord, 8, TTime);         // set the time (as text) into field #8
     :SaveEntry(DbRecord);                            // save the record
end;

local rTime := :GetField(CurrentDbEntry,8);        // get field #8 in the current record on screen
     :Show(:FormDateString(:toNumber(rTime)));     // show the formatted date on the screen, first changing the date to a number
 



The above sets the current time into a field (#8) in the current screens database when a New record is made. It shows that date formatted (YY/MM/DD or whatever you have the Date format set to) on the screen where the NS button is. You don't put field #8 on the screen, if you do , it will show up as a number.

It also demonstrates two ways to access the current record on the screen. First by using the var: CurrentDbCursor  and then by using the var: CurrentDbEntry.


-----------------
Example #2: Display the Date Calendar to pick a date.
Caution ! This apears to eat heap and also leave it fragmented !!!

Update #1 : Only (on a MP2K) when there is not an edit view open (or some other app) to accept the text (via postkeyString()) does it eat heap.

Update #2 : There was extra text is the first line: "(sd)" right after "self.pickActionScript". The below is now correct.

self.pickActionScript  := func(sd)    // this is callback, it is not directly executed
begin
   :Show(:formdatestring(sd[0]));        // show the date picked in the NS button
   postkeyString('viewFrontKey,:toString(sd[0]));   // put the date as text into any field on the screen that will accept text - ie and open edit field
// 'sd[0]' is a date number so it could be converted to text and placed in a Db field and saved

   inherited:?pickActionScript (sd);     // call the internal script
   NsVar.a:=nil;                         // remove (most) storage
end;


self.pickCancelledScript := func()      // this is callback, it is not directly executed
begin
   inherited:?pickCancelledScript();    // call the internal script
   NsVar.a:=nil;                        // remove (most) storage
end;

local p := @317;                        // internal magic pointer for a Date Popup
NsVar.a :=buildContext(p);              // make it into a view and save a reference to it

  // this bounds frame can be used to make the date popup show on the left side of the screen
//local w:= {top:5,left:5,bottom:6,right:6};

// or the bounds frame below will show the date popup on the left side
local w :=getroot():globalbox();
NsVar.a:new([time()],w, self);        // show the date popup


----------
Example #3 - Calculate a total of a field. Set the script to always run

local t :=0;                                 // the var that holds tht total
if NSvar.PartTotal and not ButtonHit  then   // check if a total exists
begin
     t := NSvar.PartTotal;                   // total already exists , so use it
end;

else begin

   :Show("Calculating ...");                 // a total doesn't exist, so calculate

   local c :=:GetCursor(7, 0);               // get a cursor for Db #7
   local e:= :GetEntry(c);                   // get the first record from the Db

   while e do                                // loop while there are more records
   begin
     local nt :=:GetField(e, 2);             // get data from field #2
     t:=t+:ToNumber(nt);                     // convert to number and add to total
     e:=:NextEntry(c)                        // get next record from database
   end;
end;

:Show("Total: " & :ToString(t));           // convert total back to text
 NSvar.PartTotal := t;                      // put the number in the NS save frame
:SaveNSvar();                               // save the vars for next time

You may want to add some more initial conditions to recalculate the total based on a changed field.
----------

